<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* IO Handler
*
* Functions Regarding Input and Output and what to do with Input
* 
* Provides the Crucial Bridge Layer Between "The Game" and the Server 
* Architecture (This is our "Glue")
*/

class ioHandler {
		
	/* Utilities */
	
	public static function inputecho($msg=""){ if(false) { echo $msg."\n"; } }   /* For Testing, but not Debugging */
	
	public static function          wrap($msg=""){ return chr(0).$msg.chr(255); }
	public static function        unwrap($msg=""){ return substr($msg,1,strlen($msg)-2); }
	public static function       console($msg="",$debug){ if($debug){ echo $msg."\n"; } }
	
	
	/* The Main Command Processor (shovels off to cmdHandler) */
	
	public static function process($connector,$msg){
		$msg = ioHandler::unwrap($msg);
		ioHandler::inputecho("< ".$msg); /* For Testing, but not Debugging */
	
		cmdHandler::splitCmd($connector->user,$msg); /* This Data is Now Part of the Game */
	}
	
	/* Login Functions */
	
	public static function entryProcess($connector,$msg) {
		$msg = ioHandler::unwrap($msg);
		$tmp = get_obj_by_field_value($connector,"connector",loginHandler::$login_sessions);
		if ($tmp) {
			$tmp->login($msg);
		} else {
			$newsession = new loginHandler($connector);
			array_push(loginHandler::$login_sessions,$newsession);
			$newsession->login($msg);
		}
	}
	
	public static function onEntry($connector) {
		loginHandler::welcome($connector);
	}
	
	/* Disconnect Functions */
	
	// This comes off the Server Directly
	// In the future we will be able to leave players in the game after disconnect
	// using this function to tell the game they are "socketless"
	// But for now, we keep it simple.
	public static function unexpectedDisconnect($connector) {
			if(!isset($connector->user)) {
				echo("\n".$connector->socket." unexpected Disconnect.");
				return; };
			World::rmv_char_from_world($connector->user);
			$connector->user->in_room()->rmv_char($connector->user);
			echo("\n".$connector->user->name." unexpected Disconnect.");
	}
	
	// This is invoked by a game by directive
	public static function doDisconnect($connector) {
		World::rmv_char_from_world($connector->user);
		$connector->user->in_room()->rmv_char($connector->user);
		$connector->server->disconnect($connector->socket); 
	}
}

?>